package com.bdq.user.infrastructure.persistence.mysql.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdq.common.enums.YesOrNoEnum;
import com.bdq.user.infrastructure.persistence.mysql.mapper.UserFollowMapper;
import com.bdq.user.infrastructure.persistence.mysql.pojo.UserFollow;
import com.bdq.user.infrastructure.persistence.mysql.service.UserFollowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author malei
* @description 针对表【user_follow(用户关注关系表)】的数据库操作Service实现
* @createDate 2024-06-08 15:22:47
*/
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow>
    implements UserFollowService{

    @Resource
    private UserFollowMapper userFollowMapper;

    @Override
    public List<UserFollow> list(Long followerUserId, List<Long> followedUserIds) {
        return lambdaQuery()
                .eq(UserFollow::getFollowerUserId, followerUserId)
                .in(UserFollow::getFollowedUserId, followedUserIds)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .list();
    }

    @Override
    public UserFollow get(Long followerUserId, Long followedUserId) {
        return lambdaQuery()
                .eq(UserFollow::getFollowerUserId, followerUserId)
                .eq(UserFollow::getFollowedUserId, followedUserId)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .one();
    }

    @Override
    public Long getFollowerNum(Long userId) {
        return lambdaQuery()
                .eq(UserFollow::getFollowerUserId, userId)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .count();
    }

    @Override
    public Long getFollowedNum(Long userId) {
        return lambdaQuery()
                .eq(UserFollow::getFollowedUserId, userId)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .count();
    }

    @Override
    public Long getEachFollowerNum(Long userId) {
        // 获取用户关注的人的ID列表
        List<Long> followedUserIds = lambdaQuery()
                .eq(UserFollow::getFollowerUserId, userId)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .list()
                .stream()
                .map(UserFollow::getFollowedUserId)
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(followedUserIds)) {
            return 0L;
        }

        return lambdaQuery()
                .eq(UserFollow::getFollowedUserId, userId)
                .in(UserFollow::getFollowerUserId, followedUserIds)
                .eq(UserFollow::getIsDeleted, YesOrNoEnum.NO.getCode())
                .count();
    }

    @Override
    public IPage<UserFollow> pageFollower(Long userId, Integer pageNo, Integer pageSize) {
        Page<UserFollow> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        return userFollowMapper.pageFollower(page, userId);
    }

    @Override
    public IPage<UserFollow> pageFollowed(Long userId, Integer pageNo, Integer pageSize) {
        Page<UserFollow> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        return userFollowMapper.pageFollowed(page, userId);
    }

    @Override
    public IPage<UserFollow> pageEachFollower(Long userId, Integer pageNo, Integer pageSize) {
        Page<UserFollow> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        return userFollowMapper.pageEachFollower(page, userId);
    }

}




